#include "coinbutton.h"

CoinButton::CoinButton(QWidget *parent) : QPushButton(parent)
{
    //初始化状态
    this->stat = 1;
    //设置按钮不规则样式
    this->setStyleSheet("QPushButton{border:0px}");

    //计时器事件  每30ms更换一次图片
    connect(&this->timer, &QTimer::timeout, [=](){
        this->setIcon(QIcon(QString(":/res/Coin000%1.png").arg(this->fram)));
        if(this->stat == 0)
        {
            this->fram++;
        }else
        {
            this->fram--;
        }
        if(this->fram == 9 || this->fram == 0)
        {
            timer.stop();
        }
    });
}

int CoinButton::getStat() const
{
    return stat;
}

void CoinButton::setStat(int value)
{
    stat = value;
    //根据value去显示不同的硬币
    if(this->stat){
        this->setIcon(QIcon(":/res/Coin0001.png"));
    }else{
        this->setIcon(QIcon(":/res/Coin0008.png"));
    }
    this->setIconSize(this->size());
}

void CoinButton::setStatWithANimation(int stat)
{
    this->stat = stat;
    if(this->stat == 0)//金币翻银币
    {
        this->fram = 1;
    }
    else if(this->stat == 1)//银币翻金币
    {
        this->fram = 8;
    }
    this->timer.start(20);
}

void CoinButton::filp()
{
    this->setStatWithANimation(!this->getStat());
}

void CoinButton::paintEvent(QPaintEvent *ev)
{
    //画出毛边背景
    QPainter painter(this);
    QPixmap pm;
    pm.load(":/res/BoardNode.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pm);
    QPushButton::paintEvent(ev);
}
